package com.wxsoft.business.physic.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wxsoft.business.common.controller.BaseController;
import com.wxsoft.business.model.easyui.DataGrid;
import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.physic.pojo.PsPhysicDict;
import com.wxsoft.business.physic.service.IPsPhysicDictService;
import com.wxsoft.business.sys.pojo.SysUser;
import com.wxsoft.util.JsonUtil;
import com.wxsoft.util.UserUtil;

@Controller
@RequestMapping("/psPhysicDict")
public class PsPhysicDictController extends BaseController{
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private IPsPhysicDictService service;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "physic/psPhysicDictlist";
	}
	
    @ResponseBody
	@RequestMapping("/datagrid")
	public DataGrid list(HttpServletRequest request,PageHelper page, PsPhysicDict psPhysicDict,HttpServletResponse response) throws IOException{
		
		DataGrid dg = new DataGrid();
		dg.setTotal(service.findCount(psPhysicDict));
		List<PsPhysicDict> psPhysicDictList = service.findAll(page, psPhysicDict);
		dg.setRows(psPhysicDictList);

		return dg ;
	}
	
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void add(HttpServletRequest request,HttpServletResponse response, PsPhysicDict psPhysicDict) {
		try {
			SysUser user = UserUtil.getUser(request);
			psPhysicDict.setOperator(user.getUser_id());
			psPhysicDict.setOperate_date(new Date());
			String r = service.add(psPhysicDict);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response,false, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public void modify(HttpServletRequest request,HttpServletResponse response, PsPhysicDict psPhysicDict) {
		try {
			SysUser user = UserUtil.getUser(request);
			psPhysicDict.setOperator(user.getUser_id());
			psPhysicDict.setOperate_date(new Date());
			String r = service.modify(psPhysicDict);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(HttpServletRequest request,HttpServletResponse response, PsPhysicDict psPhysicDict) {
		try {
			String r = service.delete(psPhysicDict);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}

	@RequestMapping(value = "/getByJson")
	public void getByJson(HttpServletRequest request,HttpServletResponse response, PsPhysicDict psPhysicDict) {
		try {
			List<PsPhysicDict> ppdL = service.getBy(psPhysicDict);
			JsonUtil.toResponseNOPage(response, ppdL);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, e.getMessage());
		}
	}
}
